//
//  AddressManage.swift
//  QCFM2
//
//  Created by iMac on 16/1/2.
//  Copyright © 2016年 kuai. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
import KeychainSwift

class AddressManage: UIViewController {
    
    @IBOutlet weak var name: UITextField!
    @IBOutlet weak var cellphone: UITextField!
    @IBOutlet weak var country: UITextField!
    @IBOutlet weak var city: UITextField!
    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var zipcode: UITextField!
    
    let keychain = KeychainSwift(keyPrefix: "myTestKey_")
    
    @IBAction func saveAddress(sender: AnyObject) {
        //
        print(self.name.text)
        print(self.cellphone.text)
        print(self.country.text)
        print(self.city.text)
        print(self.address.text)
        print(self.zipcode.text)
        
        if self.name.text == "" || self.cellphone.text == "" ||
            self.country.text == "" || self.city.text == "" ||
            self.address.text == "" || self.zipcode.text == ""
        {
            //不可以提交
        }else {
            //可以提交
            var parameters = [
                "userID": keychain.get("registerUserID")!,
                "name": self.name.text!,
                "phoneNumber": self.cellphone.text!,
                "province": self.country.text!,
                "city": self.city.text!,
                "addr": self.address.text!,
                "postNum": self.zipcode.text!
            ]
            
            let BaseUrl = "http://42.62.18.210:8090/apiradio/ar/addAddr"
            Alamofire.request(.POST, BaseUrl, parameters:parameters).responseJSON { response in
                if let result = response.result.value {
                    print(result)
                    let json = JSON(result)
                    if json["msg"] == "200" {
                        //跳转到之前界面
                        self.navigationController?.popViewControllerAnimated(true)
                    }
                }
            }
            
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.name.resignFirstResponder()
        self.cellphone.resignFirstResponder()
        self.country.resignFirstResponder()
        self.city.resignFirstResponder()
        self.address.resignFirstResponder()
        self.zipcode.resignFirstResponder()
    }
}
